#!/bin/bash

#=======================================================================================
# example script to download, build, install, & test:
#     openblas, kokkos, gtest, tines, tchem, csplib
#=======================================================================================
# User configuration  -- begin

# example:
# set CUDA="ON" to use GPU, else set to "OFF"
# JFLAG is for makefile compilation on multiple cores, here 4
# if CUDA="ON", make sure nvcc is in your system PATH

MY_CC=gcc
MY_CXX=g++
MY_FC=gfortran
JFLAG="-j 40"
CUDA="ON"

# will clone repos under REPO_BASE
# will build under BUILD_BASE
# will install under INSTALL_BASE
# example: as follows under .


REPO_BASE=${PWD}/
BUILD_BASE=${PWD}/build
INSTALL_BASE=${PWD}/install

# User configuration  -- end
#=======================================================================================

#=======================================================================================
# OpenBLAS
# nb. to make sure this openblas gets used when running outside this script
# add in your .bashrc/.bash_profile :
# on mac need this
#    export LIBRARY_PATH="${LIBRARY_PATH}:${OPENBLAS_INSTALL_PATH}/lib"
# on linux need this
#    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${OPENBLAS_INSTALL_PATH}/lib"

clean_openblas (){
echo "Cleaning OpenBLAS:"
cd ${OPENBLAS_REPOSITORY_PATH}
make clean
}

build_openblas (){
echo "Building OpenBLAS:"
cd ${OPENBLAS_REPOSITORY_PATH}
make CC=${MY_CC} FC=${MY_FC} HOSTCC=${MY_CC}
}

install_openblas (){
echo "Installing OpenBLAS:"
cd ${OPENBLAS_REPOSITORY_PATH}
make CC=${MY_CC} FC=${MY_FC} HOSTCC=${MY_CC} PREFIX=${OPENBLAS_INSTALL_PATH} install
}
#=======================================================================================

#=======================================================================================
# kokkos
build_install_kokkos(){
echo "Building kokkos:"
mkdir ${KOKKOS_BUILD_PATH}
mkdir ${KOKKOS_INSTALL_PATH}
cd ${KOKKOS_BUILD_PATH}
# kokkos install bin directory is not yet created
cmake \
    -D CMAKE_INSTALL_PREFIX=${KOKKOS_INSTALL_PATH} \
    -D CMAKE_CXX_COMPILER=${KOKKOS_CXX_REPO_COMPILER} \
    -D CMAKE_CXX_FLAGS="-fopenmp -g" \
    -D Kokkos_ENABLE_SERIAL=ON \
    -D Kokkos_ENABLE_OPENMP=ON \
    -D Kokkos_ENABLE_CUDA=${CUDA} \
    -D Kokkos_ENABLE_DEPRECATED_CODE=OFF \
    -D Kokkos_ENABLE_CUDA_CONSTEXPR=${CUDA} \
    -D Kokkos_ENABLE_CUDA_LAMBDA=${CUDA} \
    ${KOKKOS_REPOSITORY_PATH}
make ${JFLAG} install
}
#=======================================================================================

#=======================================================================================
# gtest

build_install_gtest(){
echo "Building gtest:"
mkdir ${GTEST_BUILD_PATH}
mkdir ${GTEST_INSTALL_PATH}
cd ${GTEST_BUILD_PATH}
cmake \
    -D CMAKE_INSTALL_PREFIX="${GTEST_INSTALL_PATH}" \
    -D CMAKE_CXX_COMPILER="${MY_CXX}"  \
    ${GTEST_REPOSITORY_PATH}
make ${JFLAG} install
}
#=======================================================================================

#=======================================================================================
# Tines

build_install_tines(){
echo "Building tines:"
mkdir ${TINES_BUILD_PATH}
mkdir ${TINES_INSTALL_PATH}
cd ${TINES_BUILD_PATH}
cmake \
    -D CMAKE_INSTALL_PREFIX="${TINES_INSTALL_PATH}" \
    -D CMAKE_CXX_COMPILER="${KOKKOS_CXX_COMPILER}" \
    -D CMAKE_CXX_FLAGS="-g" \
    -D CMAKE_C_COMPILER="${MY_CC}" \
    -D CMAKE_EXE_LINKER_FLAGS="-lgfortran" \
    -D TINES_ENABLE_DEBUG=OFF \
    -D TINES_ENABLE_VERBOSE=OFF \
    -D TINES_ENABLE_TEST=ON \
    -D TINES_ENABLE_EXAMPLE=ON \
    -D KOKKOS_INSTALL_PATH="${KOKKOS_INSTALL_PATH}" \
    -D GTEST_INSTALL_PATH="${GTEST_INSTALL_PATH}" \
    -D OPENBLAS_INSTALL_PATH="${OPENBLAS_INSTALL_PATH}" \
    -D YAML_INSTALL_PATH="${YAML_INSTALL_PATH}" \
    ${TINES_REPOSITORY_PATH}/src
make ${JFLAG} install
# if using e.g. yum or apt-get installed openblas, may need this:
# Not needed when using above installed openblas and macport version on osx
#    -D LAPACKE_INSTALL_PATH="${LAPACKE_INSTALL_PATH}" \
}
#=======================================================================================

#=======================================================================================
# TChem

build_install_tchem(){
echo "Building TChem:"
mkdir ${TCHEM_BUILD_PATH}
mkdir ${TCHEM_INSTALL_PATH}
cd ${TCHEM_BUILD_PATH}
cmake \
    -D CMAKE_INSTALL_PREFIX="${TCHEM_INSTALL_PATH}" \
    -D CMAKE_CXX_COMPILER="${KOKKOS_CXX_COMPILER}" \
    -D CMAKE_C_COMPILER="${MY_CC}" \
    -D CMAKE_EXE_LINKER_FLAGS="-lgfortran" \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D TCHEM_ENABLE_VERBOSE=OFF \
    -D TCHEM_ENABLE_TEST=ON \
    -D TCHEM_ENABLE_MAIN=OFF \
    -D TCHEM_ENABLE_EXAMPLE=ON \
    -D KOKKOS_INSTALL_PATH="${KOKKOS_INSTALL_PATH}" \
    -D TINES_INSTALL_PATH="${TINES_INSTALL_PATH}" \
    -D GTEST_INSTALL_PATH="${GTEST_INSTALL_PATH}" \
    ${TCHEM_REPOSITORY_PATH}/src
make ${JFLAG} install
}
#=======================================================================================

#=======================================================================================
# CSPlib

build_install_csp(){
echo "Building CSPlib:"
mkdir ${CSPlib_BUILD_PATH}
mkdir ${CSPlib_INSTALL_PATH}
cd ${CSPlib_BUILD_PATH}
cmake \
    -D CMAKE_INSTALL_PREFIX="${CSPlib_INSTALL_PATH}" \
    -D CMAKE_CXX_COMPILER="${KOKKOS_CXX_COMPILER}" \
    -D CMAKE_C_COMPILER="${MY_CC}" \
    -D CMAKE_EXE_LINKER_FLAGS="-lgfortran" \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D TCHEM_INSTALL_PATH="${TCHEM_INSTALL_PATH}"\
    -D KOKKOS_INSTALL_PATH="${KOKKOS_INSTALL_PATH}" \
    -D TINES_INSTALL_PATH="${TINES_INSTALL_PATH}" \
    -D CSPLIB_ENABLE_TEST=ON \
    -D GTEST_INSTALL_PATH="${GTEST_INSTALL_PATH}" \
    ${CSPlib_REPOSITORY_PATH}/src
make ${JFLAG} install
}
# yaml 

build_install_yaml(){
echo "Building gtest:"
mkdir ${YAML_BUILD_PATH}
mkdir ${YAML_INSTALL_PATH}
cd ${YAML_BUILD_PATH}
cmake \
    -D CMAKE_INSTALL_PREFIX="${YAML_INSTALL_PATH}" \
    -D CMAKE_CXX_COMPILER="${MY_CXX}"  \
    -D CMAKE_C_COMPILER="${MY_CC}" \
    -D CMAKE_CXX_FLAGS="-g -c" \
    -D CMAKE_EXE_LINKER_FLAGS="" \
    -D CMAKE_BUILD_TYPE=RELEASE \
    ${YAML_REPOSITORY_PATH}
make ${JFLAG} install
}
#=======================================================================================
#=======================================================================================
# main
## clone repo


mkdir -p ${REPO_BASE}
mkdir -p ${BUILD_BASE}
mkdir -p ${INSTALL_BASE}

git clone https://github.com/sandialabs/CSPlib.git
CSPlib_REPOSITORY_PATH=${REPO_BASE}/CSPlib
## Getting submodules
echo "Getting submodules..."
cd ${CSPlib_REPOSITORY_PATH}
git submodule update --init --recursive

TCHEM_REPOSITORY_PATH=${CSPlib_REPOSITORY_PATH}/ext/TChem
TINES_REPOSITORY_PATH=${TCHEM_REPOSITORY_PATH}/ext/Tines
KOKKOS_REPOSITORY_PATH=${TINES_REPOSITORY_PATH}/ext/kokkos
OPENBLAS_REPOSITORY_PATH=${TINES_REPOSITORY_PATH}/ext/OpenBLAS
GTEST_REPOSITORY_PATH=${TINES_REPOSITORY_PATH}/ext/gtest
YAML_REPOSITORY_PATH=${TINES_REPOSITORY_PATH}/ext/yaml

OPENBLAS_INSTALL_PATH=${INSTALL_BASE}/openblas
#clean_openblas
build_openblas
install_openblas
#
KOKKOS_BUILD_PATH=${BUILD_BASE}/kokkos
KOKKOS_INSTALL_PATH=${INSTALL_BASE}/kokkos
if [ "${CUDA}" = "ON" ]; then
    KOKKOS_CXX_COMPILER="${KOKKOS_INSTALL_PATH}/bin/nvcc_wrapper"
    KOKKOS_CXX_REPO_COMPILER="${KOKKOS_REPOSITORY_PATH}/bin/nvcc_wrapper"
else
    KOKKOS_CXX_COMPILER="${MY_CXX}"
    KOKKOS_CXX_REPO_COMPILER="${MY_CXX}"
fi
build_install_kokkos

GTEST_BUILD_PATH=${BUILD_BASE}/gtest
GTEST_INSTALL_PATH=${INSTALL_BASE}/gtest
build_install_gtest

YAML_BUILD_PATH=${BUILD_BASE}/yaml
YAML_INSTALL_PATH=${INSTALL_BASE}/yaml
build_install_yaml

TINES_BUILD_PATH=${BUILD_BASE}/tines
TINES_INSTALL_PATH=${INSTALL_BASE}/tines
build_install_tines

TCHEM_BUILD_PATH=${BUILD_BASE}/tchem
TCHEM_INSTALL_PATH=${INSTALL_BASE}/tchem
build_install_tchem

CSPlib_BUILD_PATH=${BUILD_BASE}/csplib
CSPlib_INSTALL_PATH=${INSTALL_BASE}/csplib
build_install_csp

exit
